{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### image和label一一对应"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "train_path = './mnist_image_label/mnist_train_jpg_60000/'\n",
    "train_txt = './mnist_image_label/mnist_train_jpg_60000.txt'\n",
    "x_train_savepath = './mnist_image_label/mnist_x_train.npy'\n",
    "y_train_savepath = './mnist_image_label/mnist_y_train.npy'\n",
    "\n",
    "test_path = './mnist_image_label/mnist_test_jpg_10000/'\n",
    "test_txt = './mnist_image_label/mnist_test_jpg_10000.txt'\n",
    "x_test_savepath = './mnist_image_label/mnist_x_test.npy'\n",
    "y_test_savepath = './mnist_image_label/mnist_y_test.npy'\n",
    "\n",
    "\n",
    "def generateds(path, txt):\n",
    "    f = open(txt, 'r')  # 以只读形式打开txt文件\n",
    "    contents = f.readlines()  # 读取文件中所有行\n",
    "    f.close()  # 关闭txt文件\n",
    "    x, y_ = [], []  # 建立空列表\n",
    "    for content in contents:  # 逐行取出\n",
    "        value = content.split()  # 以空格分开，图片路径为value[0] , 标签为value[1] , 存入列表\n",
    "        img_path = path + value[0]  # 拼出图片路径和文件名\n",
    "        img = Image.open(img_path)  # 读入图片\n",
    "        img = np.array(img.convert('L'))  # 图片变为8位宽灰度值的np.array格式\n",
    "        img = img / 255.  # 数据归一化 （实现预处理）\n",
    "        x.append(img)  # 归一化后的数据，贴到列表x\n",
    "        y_.append(value[1])  # 标签贴到列表y_\n",
    "        print('loading : ' + content)  # 打印状态提示\n",
    "\n",
    "    x = np.array(x)  # 变为np.array格式\n",
    "    y_ = np.array(y_)  # 变为np.array格式\n",
    "    y_ = y_.astype(np.int64)  # 变为64位整型\n",
    "    return x, y_  # 返回输入特征x，返回标签y_\n",
    "\n",
    "\n",
    "if os.path.exists(x_train_savepath) and os.path.exists(y_train_savepath) and os.path.exists(\n",
    "        x_test_savepath) and os.path.exists(y_test_savepath):\n",
    "    print('-------------Load Datasets-----------------')\n",
    "    x_train_save = np.load(x_train_savepath)\n",
    "    y_train = np.load(y_train_savepath)\n",
    "    x_test_save = np.load(x_test_savepath)\n",
    "    y_test = np.load(y_test_savepath)\n",
    "    x_train = np.reshape(x_train_save, (len(x_train_save), 28, 28))\n",
    "    x_test = np.reshape(x_test_save, (len(x_test_save), 28, 28))\n",
    "else:\n",
    "    print('-------------Generate Datasets-----------------')\n",
    "    x_train, y_train = generateds(train_path, train_txt)\n",
    "    x_test, y_test = generateds(test_path, test_txt)\n",
    "\n",
    "    print('-------------Save Datasets-----------------')\n",
    "    x_train_save = np.reshape(x_train, (len(x_train), -1))\n",
    "    x_test_save = np.reshape(x_test, (len(x_test), -1))\n",
    "    np.save(x_train_savepath, x_train_save)\n",
    "    np.save(y_train_savepath, y_train)\n",
    "    np.save(x_test_savepath, x_test_save)\n",
    "    np.save(y_test_savepath, y_test)\n",
    "\n",
    "model = tf.keras.models.Sequential([\n",
    "    tf.keras.layers.Flatten(),\n",
    "    tf.keras.layers.Dense(128, activation='relu'),\n",
    "    tf.keras.layers.Dense(10, activation='softmax')\n",
    "])\n",
    "\n",
    "model.compile(optimizer='adam',\n",
    "              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),\n",
    "              metrics=['sparse_categorical_accuracy'])\n",
    "\n",
    "model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)\n",
    "model.summary()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
